//后端数据统计仪表板
const router = require('koa-router')()
// 生成雪花ID的方法
const createSnow = function (ctx) {
    let snowFlake = ctx.snowFlake;
    let snowId = snowFlake.createSnow(4);
    return snowId;
}
router.prefix('/dashboard')
router.post("/addlist", async (ctx, next) => {//新增todo
    let obj = ctx.request.body;
    let text = obj.text;
    let done = obj.done;
    let id = ctx.snowFlake.createSnow(text, done);
    let sql = 'insert into todolist (id,text,done) values (?,?,?)';
    let ress = await ctx.db.EXCUTE(sql, [id, text, done]);
    let sqls = 'select * from todolist';
    let res = await ctx.db.EXCUTE(sqls);
    console.log(ress, "插入todo");
    let options;
    options = res.map(item => {
        return {
            ...item, done: Boolean(Number(item.done))//强制类型转换
        }
    });
    console.log(id);
    ctx.body = { code: 200, msg: "success", res: options }
})
router.post("/deletelist", async (ctx, next) => {//删除todo
    let id = ctx.request.body.id;
    console.log(id);
    let sql = 'delete from todolist where id=?'
    let ress = await ctx.db.EXCUTE(sql, [id]);
    let sqls = 'select * from todolist';
    let res = await ctx.db.EXCUTE(sqls);
    console.log(ress, "插入todo");
    let options;
    options = res.map(item => {
        return {
            ...item, done: Boolean(Number(item.done))//强制类型转换
        }
    });
    console.log(id);
    ctx.body = { code: 200, msg: "success", res: options }
})
router.post("/checklist", async (ctx, next) => {//选中todo切换布尔值
    let id = ctx.request.body.id;
    let done = ctx.request.body.done;
    console.log(done);
    done = String(Number(!done));
    let sql = 'update todolist set done = ? where id=?'
    let ress = await ctx.db.EXCUTE(sql, [done, id]);
    let sqls = 'select * from todolist';
    let res = await ctx.db.EXCUTE(sqls);
    console.log(ress, "插入todo");
    let options;
    options = res.map(item => {
        return {
            ...item, done: Boolean(Number(item.done))//强制类型转换
        }
    });
    console.log(id);
    ctx.body = { code: 200, msg: "成功", res: options }
})
router.get('/getlist', async (ctx, next) => {//初步渲染
    let options;
    // let resList = [];
    let sql = 'select * from todolist';
    let res = await ctx.db.EXCUTE(sql);
    //     res = JSON.stringify(res);
    // res = JSON.parse(res);
    console.log(res, "todolist");
    options = res.map(item => {
        return {
            ...item, done: Boolean(Number(item.done))//强制类型转换
        }
    });
    console.log(options);
    ctx.body = { code: 200, res: options }

})
router.get('/getcollege', async (ctx, next) => {//初步渲染

    let sql = 'select * from institute';
    let res = await ctx.db.EXCUTE(sql);
    //     res = JSON.stringify(res);
    // res = JSON.parse(res);
    console.log(res, "学院列表");

    ctx.body = { code: 200, res: res, msg: "获取学院信息成功" }

})
router.post('/getteacher', async (ctx, next) => {//初步渲染
    let id = ctx.request.body.id;
    console.log(id);
    let sql = 'select * from teacher where institute_id = ?';
    let res = await ctx.db.EXCUTE(sql, [id]);
    //     res = JSON.stringify(res);
    // res = JSON.parse(res);
    console.log(res, "教师列表");
    ctx.body = { code: 200, res: res, msg: "获取教师信息成功" }

})
router.post('/getcourse', async (ctx, next) => {//初步渲染
    let id = ctx.request.body.id;
    console.log(id);
    let sum;
    let sql = 'select * from course where teacher_id = ?';
    let res = await ctx.db.EXCUTE(sql, [id]);
    sum = JSON.stringify(res);
    sum = JSON.parse(sum);
    // console.log(sum);
    let list = sum.map((item) => {
        return {
            forid: item.course_id,

        };
    })
    let arr = [];
    for (var i = 0; i < list.length; i++) {
        console.log(list[i].forid);
        let id = list[i].forid;
        let sqls = 'select * from student_course_relation where course_id = ?';
        let ress = await ctx.db.EXCUTE(sqls, [id]);
        console.log(ress);
        let count = ress.length;
        arr.push(count);

    }
    console.log(arr);
    console.log(list);
    // console.log(res,"课程列表");
    ctx.body = { code: 200, res: res, msg: "获取课程信息成功", arr: arr }

})
router.post('/getWork', async (ctx, next) => {//初步渲染
    let id = ctx.request.body.id;
    console.log(id);
    let sql = 'select * from assignment where course_id = ?';
    let sqls = 'select course_name from course where course_id = ?'
    let res = await ctx.db.EXCUTE(sql, [id]);
    let nameone = await ctx.db.EXCUTE(sqls, [id])
    //     res = JSON.stringify(res);
    // res = JSON.parse(res);
    console.log(res, "任务列表");
    ctx.body = { code: 200, res: res, msg: "获取任务信息成功", nameone: nameone }

})
// const getTransition=function(e){
//     e = JSON.stringify(e);
//     e = JSON.parse(e);
//     return e;
// }
router.post('/getDone', async (ctx, next) => {//初步渲染
    let id = ctx.request.body.id;
    console.log(id);
    let sqlone = 'select * from student_submit_assignment where assignment_id=? and submit_status=?';
    let resone = await ctx.db.EXCUTE(sqlone, [id, 'unpublish']);
    resone = resone.length;
    let sqltwo = 'select * from student_submit_assignment where assignment_id=? and submit_status=?';
    let restwo = await ctx.db.EXCUTE(sqltwo, [id, 'published']);
    restwo = restwo.length;
    let sqlthree = 'select * from student_submit_assignment where assignment_id=? and submit_status=?';
    let resthree = await ctx.db.EXCUTE(sqlthree, [id, 'draft']);
    resthree = resthree.length;
    let sql = 'select assignment_title from assignment where assignment_id=?';
    let nametwo = await ctx.db.EXCUTE(sql, [id])
    console.log(resone, restwo, resthree, "数字");
    ctx.body = { code: 200, msg: "获取任务信息成功", unpublish: resone, published: restwo, draft: resthree, nametwo: nametwo }

})
module.exports = router